" no compatible with vi
set nocompatible
set nu
set nowrap
set expandtab

set ignorecase
set smartcase
" do incremental searching
" set incsearch
" search highlights
set hls

" display incomplete commands
set showcmd 

"
set splitright

" ------------{--------------
" 离开时自动保存 vim 状态
autocmd VimLeave * mks! $HOME/vimfiles/session/Session.vim

" 启动 vim 时自动读取 session 文件, 目前重新定义 vim.bat
" autocmd VimEnter * :call s:ReadSession()
" function s:ReadSession()
" 	let session_file = $HOME . "/vimfiles/session/Session.vim"
" 	if filereadable( session_file )
" 		execute "so " . session_file
" 	endif
" endfunction
" ------------}--------------

" 设置 vim 内部编码
" set encoding=utf-8

" 总是显示状态栏
set laststatus=2

" 状态栏
set statusline=%<%f\ %h%m%r%=%{\"[\".(&fenc==\"\"?&enc:&fenc).((exists(\"+bomb\")\ &&\ &bomb)?\",B\":\"\").\"]\ \"}%k\ %-14.(%l,%c%V%)\ %P

" default fileencoding
set fenc=gbk
set fileencodings=ucs-bom,utf-8,GB18030,gbk

" do C-style indenting
" set cindent

" see help (complex)
set fo=tcrqn

" tab spacing (settings below are just to unify it)
set tabstop=4
" unify
set softtabstop=4
" unify
set shiftwidth=4

"
set textwidth=0

" 后退删除
" set backspace=indent,eol,start
set backspace=indent,start

" 光标位置
set startofline

" 折叠
set foldcolumn=1

" 当前文件所在目录
map <silent> <Leader>cd :cd %:h<CR>

" 打开 cmd
map <silent> <Leader>st :!start<CR>

" 剪切版
" Vim will use the clipboard register '*' for all yank, delete, change and put operations which would normally go to the unnamed register
" 在 windows 下 "+ 和 "* 都为系统剪切板
" set clipboard+=unnamed
map 'y "+y

" 快速移动
map <c-n> 5j
map <c-p> 5k

" make PHP
map <silent> <Leader>ma :compiler myphp<CR> :make<CR> :cw<CR> <C-W>L

" set cursorline

" Convenient command to see the difference between the current buffer and the
" file it was loaded from, thus the changes you made.
" Only define it when not defined already.
if !exists(":DiffOrig")
command DiffOrig vert new | set bt=nofile | r # | 0d_ | diffthis
  \ | wincmd p | diffthis
endif

syntax on

" enable file type detection
filetype on
filetype plugin on
filetype indent on

" autocomplete
" autocmd FileType html set omnifunc=htmlcomplete#CompleteTags | set et
" autocmd FileType css set omnifunc=csscomplete#CompleteCSS | set et
" autocmd FileType javascript set omnifunc=javascriptcomplete#CompleteJS | set et
" autocmd FileType php set omnifunc=phpcomplete#CompletePHP | set et

" colorscheme slate

" -------------------------------------
" map {
"
" 搜索选中
vnoremap <silent> ,/ y/<C-R>=escape(@", '\\/.*$^~[]')<CR><CR>
vnoremap <silent> ,? y?<C-R>=escape(@", '\\/.*$^~[]')<CR><CR>

" 打开当前文件的目录
" map <silent> ,o :Explore<CR><CR>
map <silent> ,s :Sexplore<CR><CR>
map <silent> ,q :close<CR><CR>

"
map <silent> ,> :vert res<CR>
map <silent> ,< :res<CR>
map <silent> ,, <C-W>=

" 插入当前日期
nnoremap <F5> "=strftime("%c")<CR>P
inoremap <F5> <C-R>=strftime("%c")<CR>
"
" }
" -------------------------------------

" Ubuntu 下设置
" xterm 颜色不全, see :h xterm-color
" if &term =~ "xterm"
" if has("terminfo")
" set t_Co=8
" set t_Sf=<Esc>[3%p1%dm
" set t_Sb=<Esc>[4%p1%dm
" else
" set t_Co=8
" set t_Sf=<Esc>[3%dm
" set t_Sb=<Esc>[4%dm
" endif
" endif
" 括号匹配颜色
" autocmd BufEnter * :hi MatchParen cterm=bold ctermfg=blue ctermbg=white

" HTML5 模板替换, 
iab html5 <!doctype html><CR><html><CR><head><CR><meta charset="gbk" /><CR><title></title><CR><link rel="stylesheet" href="css_example_url" /><CR><script src="js_example_url"></script><CR></head><CR><body><CR><div><CR></div><CR><script><CR>/*hello world*/<CR></script><CR></body><CR></html><Esc>6k

" vim 中删除文件
" command mode :Remove %
command! -complete=file -nargs=1 Remove :echo 'Remove: '.'<f-args>'.' '.(delete(<f-args>) == 0 ? 'SUCCEEDED' : 'FAILED')

" -------------------------------------
" plugin 相关设置 {
"
" taglist 插件之显示一个文件
" let Tlist_Show_One_File=1

" BufferExplore 
" Split left.
let g:bufExplorerSplitRight=0        
" map <silent> ,be \be
" map <silent> ,bv \bv
let g:bufExplorerShowTabBuffer=0        " Yes.

" word_complete 插件
" autocmd BufEnter * call DoWordComplete() 

" Conque 插件
let g:ConqueTerm_InsertOnEnter = 0

" NERDTree 插件
map <silent> ,v :NERDTree<CR>
" }

" ScratchOpen
map <silent> <Leader>so :ScratchOpen<CR>
" -------------------------------------


" YUICompressor
function! YUICompressor()
    " let yuicompressor_program = 'java -jar D:/tools/ourtools/yui-compressor/yuicompressor.jar --verbose'
    let yuicompressor_program = 'java -jar D:/tools/ourtools/yui-compressor/yuicompressor.jar'
    "
    let yui_option_charset = '--charset ' . &fileencoding
    "
    let current_file = expand('%:p')
    if match(current_file, "\.js$") != -1
        let yui_option_type = '--type js'
    elseif match(current_file, "\.css$") != -1
        let yui_option_type = '--type css'
    else
        echoerr 'Error: File "' . current_file . '" is not js or css!'
        return
    endif
    "
    let yui_option_outputfile = substitute(current_file, '\.\(js\|css\)$', '-min\.\1', "")
    if yui_option_outputfile == current_file
        echoerr 'Error: Cannot substitute filename to "-min" file!'
        return
    endif
    "
    let yui_option_outputfile = '-o ' . shellescape(yui_option_outputfile)
    let current_file = shellescape(current_file)
    "
    "
    let yui_options = yui_option_charset . ' ' . yui_option_type . ' ' . yui_option_outputfile . ' ' . current_file
    let cmd = yuicompressor_program . ' ' . yui_options
    "
    "" echo cmd
    "" return
    "
    let cmd_output = system(cmd)
    "
    echo cmd_output
endfunction


" colorscheme darkblue
" vim 脚本练习
" 根据时间改变颜色 {
let g:FavColorSchemes = [ 'darkblue', 'morning', 'shine', 'evening']
function SetTimeOfDayColor()
  colorscheme default
	" 加零是为了保证函数返回值是 数字类型
	let CurrentIndex = ( strftime( '%H') + 0 ) / 6
	if g:colors_name !~ g:FavColorSchemes[ CurrentIndex]
		execute 'colorscheme ' . g:FavColorSchemes[ CurrentIndex]
	endif
endfunction
" }
"
